#region File Description
//-----------------------------------------------------------------------------
// RelativeSprite.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion

#region Using Statements
using System;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
#endregion

namespace BrainWare
{
    /// <summary>
    /// This class provides helper functions for making sprites, which are pixel based
    /// get drawn in correct positions and sizes when a window is resized.
    /// For the purposes of this code 1280x720 HD resolution is considered the base 
    /// size. Everything is sized based on its relative value to that.
    /// </summary>
    public class RelativeSpriteBatch : SpriteBatch
    {
        private const int fullHeight = 720;
        private const int fullWidth = 1280;
        private static float scale;
        private static int offset;

        /// <summary>
        /// Initializes a new instance of the <see cref="RelativeSpriteBatch"/> class.
        /// </summary>
        /// <param name="graphicsDevice">The graphics device where sprites will be 
        /// drawn.</param>
        public RelativeSpriteBatch(GraphicsDevice graphicsDevice)
            : base(graphicsDevice)
        {
            Resize();
        }

        /// <summary>
        /// Call Resize whenever the window is resized, it calculates the new ratios for
        /// all further draw calls
        /// </summary>
        public void Resize()
        {

            //Heights are just scaled to fit the current window height.
            //Widths are scaled in proportion.
            //The x offset is calculated to force the images to center themselves no 
            //matter what the aspect ratio.
            //Left as right will be clipped as required
            scale = (float)GraphicsDevice.Viewport.Height / (float)fullHeight;
            offset = (int)((GraphicsDevice.Viewport.Width - fullWidth * scale) / 2);
        }

        /// <summary>
        /// Draws a sprite at a particular position
        /// </summary>
        /// <param name="texture">The texture to take the sprite from. For this overload
        /// it will draw the entire texture.</param>
        /// <param name="position">The position in 1280x720 screen space. It will 
        /// actually be drawn in the correct relative position and size</param>
        /// <param name="color">The color to tint the sprite</param>
        public new void Draw(Texture2D texture, Vector2 position, Color color)
        {
            this.Draw(texture, position, null, color);
        }

        /// <summary>
        /// Draws a sprite at a particular position
        /// </summary>
        /// <param name="texture">The texture to take the sprite from. For this overload
        /// it will draw the entire texture.</param>
        /// <param name="position">The position in 1280x720 screen space. It will 
        /// actually be drawn in the correct relative position and size</param>
        /// <param name="source">The part of the texture to draw</param>
        /// <param name="color">The color to tint the sprite</param>
        public new void Draw(Texture2D texture, Vector2 position, Rectangle? source,
                             Color color)
        {
            //Scale and move the sprite based on current screen size 
            base.Draw(texture, new Vector2((int)((position.X * scale) + offset),
                      (int)(position.Y * scale)), source, color, 0, Vector2.Zero, scale,
                      SpriteEffects.None, 0);
        }
    }
}
